package org.lilred.nzbberry.controllers;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.InputConnection;

import org.lilred.nzbberry.http.HttpRequestDispatcher;
import org.lilred.nzbberry.http.URL;
import org.lilred.nzbberry.http.cookie.CookieManager;
import org.lilred.nzbberry.http.cookie.HttpCookie;
import org.lilred.nzbberry.models.ConfigModel;
import org.lilred.nzbberry.views.AppView;

import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.browser.field2.BrowserFieldCookieManager;
import net.rim.device.api.browser.field2.BrowserFieldRequest;
import net.rim.device.api.browser.field2.ProtocolController;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

public class BrowserController extends ProtocolController {
	BrowserField bf;
    CookieManager manager = new CookieManager();
	public BrowserController(BrowserField bf) {
		super(bf);
		this.bf = bf;
	}

	public void handleNavigationRequest(BrowserFieldRequest request) throws Exception {
		super.handleNavigationRequest(request);
		/*
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			public void run() {
				AppView.getInstance().appManager.searchButton.setFocus();
			}
		});
		String url = request.getURL();
		
		//Prepare the connection and the headers
		HttpConnection connection = (HttpConnection) Connector.open(url + HttpRequestDispatcher.getConnectionParameters());
        if (request.getPostData() != null) {
        	connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
            OutputStream requestOutput = connection.openOutputStream();
            requestOutput.write(request.getPostData());
            requestOutput.close();
        }
        connection.setRequestProperty("cookie", manager.get(url));
        manager.put(connection);
		//Take action based on the response code
		int responseCode = connection.getResponseCode();
		if (responseCode == HttpConnection.HTTP_OK) {
			String contentType = connection.getHeaderField("content-type");
			String disposition = connection.getHeaderField("content-disposition");
			if (disposition != null && disposition.indexOf(".nzb") > 0) {
				UiApplication.getUiApplication().invokeLater(new Runnable() {
					public void run() {
						Dialog.alert("NZB Detected!");
					}
				});
			}
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			InputStream responseData = connection.openInputStream();
			byte[] buffer = new byte[10000];
			int bytesRead = responseData.read(buffer);
			while (bytesRead > 0) {
				baos.write(buffer, 0, bytesRead);
				bytesRead = responseData.read(buffer);
			}
			baos.close();
			connection.close();

			try {
				bf.displayContent(baos.toByteArray(), contentType, url);
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
		else if (responseCode == HttpConnection.HTTP_MOVED_PERM || responseCode == HttpConnection.HTTP_MOVED_TEMP) {
			String location = connection.getHeaderField("location");
			String baseUrl = new String();
			if (location.indexOf("//") == -1) {
				//This is a relative location, we must find the base URL
				int slashPosition = url.lastIndexOf('/');
				baseUrl = url.substring(0, slashPosition + 1);
			}
			bf.requestContent(baseUrl + location);
		} else bf.displayContent("Error: Response code: " + responseCode, "");
*/
	}

	public InputConnection handleResourceRequest(BrowserFieldRequest browserFieldRequest) throws Exception {
		return super.handleResourceRequest(browserFieldRequest);
	}
}